package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.dao.MenuPermissionDao;
import com.vincent.dao.PermissionDao;
import com.vincent.dao.RolePermissionDao;
import com.vincent.model.Permission;
import com.vincent.vo.PermissionAddVo;
import com.vincent.vo.PermissionEditVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vincent on 15-1-3.
 */
@Service("permissionService")
public class PermissionService {
    @Resource
    private PermissionDao permissionDao;
    @Resource
    private MenuPermissionDao menuPermissionDao;
    @Resource
    private RolePermissionDao rolePermissionDao;

    public Map<String,Object> getPermissions(){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        List<Permission> permissions = permissionDao.selectAll();
        List<Permission> permissionList = getListByParentId(null, permissions);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),permissionList);
        return resultMap;
    }

    public static List<Permission> getListByParentId(Integer parentId,List<Permission> permissions){
        List<Permission> permissionList=new ArrayList<Permission>();
        for(Permission permission:permissions){
            Integer permissionParentId = permission.getParentId();
            if(parentId==null?null==permissionParentId:(permissionParentId!=null&&permissionParentId.intValue()==parentId.intValue())){
                List<Permission> listByParentId = getListByParentId(permission.getId().intValue(), permissions);
                permission.setChildren(listByParentId);
                permissionList.add(permission);
            }
        }
        return permissionList;

    }

    public Map<String,Object> savePermission(PermissionAddVo vo){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Permission permissionByName = permissionDao.getPermissionByName(vo.getName());
        if(permissionByName!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"权限名已存在");
            return resultMap;
        }
        if(vo.getParentId()!=null){
            Permission permissionById = permissionDao.getPermissionById(vo.getParentId());
            if(permissionById==null){
                resultMap.put(ResultMapKey.ERROR.getCode(),"上级权限不存在");
                return resultMap;
            }
        }
        Permission permission = new Permission();
        permission.setDescription(vo.getDesc());
        permission.setName(vo.getName());
        permission.setParentId(vo.getParentId());
        permissionDao.savePermission(permission);
        permissionDao.clearCache();
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"保存权限成功");
        return resultMap;
    }

    public Map<String,Object> updatePermission(PermissionEditVo vo){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Permission permissionById = permissionDao.getPermissionById(vo.getId());
        if(permissionById==null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"权限不存在");
            return resultMap;
        }
        Permission permissionByName = permissionDao.getPermissionByName(vo.getName());
        if(permissionByName!=null&&!vo.getName().equals(permissionById.getName())){
            resultMap.put(ResultMapKey.ERROR.getCode(),"权限名已存在");
            return resultMap;
        }
        Permission permission = new Permission();
        permission.setDescription(vo.getDesc());
        permission.setName(vo.getName());
        permission.setId(vo.getId());
        permissionDao.updatePermission(permission);
        permissionDao.clearCache();
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"编辑权限成功");
        return resultMap;
    }

    public Map<String,Object> getPermission(int id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Permission permission = permissionDao.getPermissionById(id);
        if(permission==null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"权限不存在");
            return resultMap;
        }
        resultMap.put(ResultMapKey.SUCCESS.getCode(),permission);
        return resultMap;
    }

    public Map<String,Object> deletePermission(Integer []ids){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        menuPermissionDao.deleteByPermissionIds(ids);
        rolePermissionDao.deleteByPermissionIds(ids);
        permissionDao.deletePermission(ids);
        permissionDao.clearCache();
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"删除权限成功");
        return resultMap;
    }
    public static Permission getPermissionById(Integer permissionId, List<Permission> permissionAllList){
        for(Permission permission:permissionAllList){
            if(permission.getId()==permissionId){
                return permission;
            }
        }
        return null;
    }
}
